﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace hedefgrup.reports.common.SSRD
{
    public abstract class Report_SSRD_Abstract : Report_Abstract
    {
        private Dictionary<string, string> cachedServices = new Dictionary<string, string>();

        protected string GetAuthorizedServiceName(string authorizedService)
        {
            if (string.IsNullOrEmpty(authorizedService))
            {
                return string.Empty;
            }

            if (!Regex.IsMatch(authorizedService, "^\\d+"))
            {
                return authorizedService;
            }

            Match dealerCodeMatch = Regex.Match(authorizedService, "^\\d+");
            string dealerCode = dealerCodeMatch.Value.TrimStart('0');

            if (cachedServices.ContainsKey(dealerCode))
            {
                return cachedServices[dealerCode];
            }

            Tree dealerNode = Context.Project.GetChild("DealerList").GetChild(dealerCode);
            if (dealerNode == null)
            {
                cachedServices.Add(dealerCode, authorizedService + " (SERVIS TANIMLI DEĞİL)");
            }
            else
            {
                cachedServices.Add(dealerCode, dealerCode + "-" + dealerNode.Text);
            }

            return cachedServices[dealerCode];
        }
    }
}
